//
//  CookTopicCustomCollectionViewCell.m
//  Delicious2
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CookTopicCustomCollectionViewCell.h"

@implementation CookTopicCustomCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    [self creatSubViews];
    
    return self;
}

- (void)setTopicModel:(CookTopicModel *)topicModel {
    if (_topicModel != topicModel) {
        _topicModel = topicModel;
    }

    [self.imageV sd_setImageWithURL:[NSURL URLWithString:topicModel.image_url] placeholderImage:[UIImage imageNamed:@"ImagePlace"]];
    self.labelTitle.text = topicModel.food_name;
    self.labelTopic.text = topicModel.descriptionTitle;
    
    //自适应高度
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:18]};
    CGRect rect = [self.labelTopic.text boundingRectWithSize:CGSizeMake(WIDTH, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.labelTopic.frame = CGRectMake(10, self.frame.size.height / 2 + 30, WIDTH - 20, rect.size.height + 40);
    
    self.scrollView.contentSize = CGSizeMake(WIDTH,self.imageV.frame.size.height + self.labelTitle.frame.size.height + self.labelTopic.frame.size.height + 64);
}

- (void)creatSubViews {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;

    self.imageV = [[UIImageView alloc] initWithFrame:CGRectZero];
    
    self.labelTitle = [[UILabel alloc] initWithFrame:CGRectZero];
    self.labelTitle.font = [UIFont boldSystemFontOfSize:22];
    self.labelTitle.textColor = kColor;
 
    
    self.labelTopic = [[UILabel alloc] initWithFrame:CGRectZero];
    self.labelTopic.font = [UIFont systemFontOfSize:18];
    self.labelTopic.numberOfLines = 0;
    
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.imageV];
    [self.scrollView addSubview:self.labelTitle];
    [self.scrollView addSubview:self.labelTopic];
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super layoutSubviews];
   
    self.imageV.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 2);
    
    self.labelTitle.frame = CGRectMake(10, self.frame.size.height/ 2 , WIDTH - 20, 40);
    
    self.scrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 50);

}

//图片跟随放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffSet = scrollView.contentOffset.y;

    if (yOffSet < 0) {
        self.imageV.frame = CGRectMake(yOffSet / 2, yOffSet, self.frame.size.width - yOffSet, self.frame.size.height / 2 - yOffSet);
    }
}

@end
